package dp.mobile.entity;

import org.w3c.dom.Node;

public class Buff {
	public static final int SPEED_MODIFIER	= 0;
	public static final int ARMOR_MODIFIER	= 1;

	public Buff() {
		//Initialize
		mName			= "Buff";
		mTint			= 0;
		mIndex			= 0;
		mTimer			= 0;
		mDuration		= 0;
		mDamage			= 0;
		mDamageDelay	= 0;
		mDamageTimer	= 0;
		mActive			= true;

		//Initialize modifier
		mModifiers							= new float[Buff.ARMOR_MODIFIER + 1];
		mModifiers[Buff.SPEED_MODIFIER]	= 1.0f;
		mModifiers[Buff.ARMOR_MODIFIER]	= 1.0f;
	}

	public Buff(Node xml) {
		//Default
		this();
        
        //Parse xml
		int childCount = xml.getChildNodes().getLength();
        for(int i = 0; i < childCount; ++i){
        	Node child = xml.getChildNodes().item(i);
			if (child.getNodeType() == Node.TEXT_NODE) continue;
        	
            String tag      = child.getNodeName();
            String value    = child.getFirstChild().getNodeValue();
            
            if(tag.equals("name"))			mName = value;
            else if(tag.equals("tint"))		mTint = Integer.parseInt(value);
			else if(tag.equals("duration"))	mDuration = Long.parseLong(value);
            else if(tag.equals("modifier")) {
            	int modifierCount = child.getChildNodes().getLength();
                for(int j = 0; j < modifierCount; ++j){
                	Node modifier 		= child.getChildNodes().item(j);
                	if (modifier.getNodeType() == Node.TEXT_NODE) continue;
                    String childTag     = modifier.getNodeName();
                    String childValue   = modifier.getFirstChild().getNodeValue();
                    
                    if(childTag.equals("speed"))        mModifiers[Buff.SPEED_MODIFIER] = Float.parseFloat(childValue);
                    else if(childTag.equals("armor"))   mModifiers[Buff.ARMOR_MODIFIER] = Float.parseFloat(childValue);
                }
            } else if(tag.equals("poison")){
            	int poisonCount = child.getChildNodes().getLength();
                for(int j = 0; j < poisonCount; ++j){
                	Node poison 		= child.getChildNodes().item(j);
                	if (poison.getNodeType() == Node.TEXT_NODE) continue;
                    String childTag     = poison.getNodeName();
                    String childValue   = poison.getFirstChild().getNodeValue();
                    
                    if(childTag.equals("delay"))        mDamageDelay = Long.parseLong(childValue);
                    else if(childTag.equals("damage"))  mDamage = Integer.parseInt(childValue);
                }
            }
        }
	}

	//Accessors
	public int getTint()		{	return mTint;		}
	public int getIndex()		{	return mIndex;		}
	public long getDuration()	{	return mDuration;		}
	public boolean isActive()	{	return mActive;	}

	public float getModifier(int index) {
		return mModifiers[index];
	}

	public void update(long time, Enemy enemy) {
		//Increase timer
		mTimer += time;
		if (mDamageDelay > 0) mDamageTimer += time;

		//Is timer more than duration
		if (mTimer >= mDuration) {
			//Deactivate
			mActive = false;
		} else {
			//Check damage timer
			if (mDamageDelay > 0 && mDamageTimer > mDamageDelay) {
				//Damage enemy
				enemy.damage(mDamage, null);

				//Reduce timer
				mDamageTimer -= mDamageDelay;
			}
		}
	}

	//Data
	protected String	mName;
	protected int		mTint;
	protected int		mIndex;
	protected boolean	mActive;
	protected long		mDuration;
	protected long		mTimer;

	//Poison
	protected int		mDamage;
	protected long		mDamageDelay;
	protected long		mDamageTimer;
	protected float[]	mModifiers;
}
